//
//  ACBaseViewModel.m
//  ACFramework
//
//  Created by caoyq on 2018/11/22.
//

#import "ACBaseViewModel.h"
#import "ACMacro.h"

@implementation ACBaseViewModel

- (instancetype)initWithViewController: (UIViewController *)viewController
{
    self = [super init];
    if (self) {
        _currentViewController = viewController;
    }
    return self;
}

#pragma mark - 刷新数据

- (void)loadData: (BOOL)isHeader success: (void (^)(BOOL haveMoreData))success failure: (void(^)(void))failure
{
    if (isHeader) {
        self.pageIndex = [self pageIndexStartValue];
    }else {
        self.pageIndex += 1;
    }
    
    [self loadDataWithSuccess:^(NSArray *list) {
        if (isHeader) {
            [self.dataSource removeAllObjects];
        }
        [self.dataSource addObjectsFromArray:list];
        //这里默认根据每页的数量来判断还有没有值，项目中可能有单独的参数，具体情况具体考虑
        BOOL haveMoreData = list.count == self.pageSize;
        if (success) {
            success(haveMoreData);
        }
    } failure:^{
        if (failure) {
            failure();
        }
    }];
}

- (void)loadDataWithSuccess: (void(^)(NSArray *list))success failure: (void(^)(void))failure
{
    //pageIndex pageSize dataSource
}

- (NSInteger)pageIndexStartValue
{
    return 0;
}

#pragma mark - Getter

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSString *)vcName
{
    return NSStringFromClass([self.currentViewController class]);
}

- (void)dealloc
{
    ACLog(@"----%s----dealloc----",object_getClassName(self));
}

@end
